package com.wyj.chat.websocket_chat.service.impl;

import com.wyj.chat.websocket_chat.dao.FriendsMapper;
import com.wyj.chat.websocket_chat.dao.UserMapper;
import com.wyj.chat.websocket_chat.pojo.Friends;
import com.wyj.chat.websocket_chat.pojo.User;
import com.wyj.chat.websocket_chat.service.FriendsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.naming.Name;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by wyj on 2018/4/11
 */
@Service
@Transactional
public class FriendsServiceImpl implements FriendsService{

    @Autowired
    private FriendsMapper friendsMapper;

    public List<Friends> getListById(String id) {
        return friendsMapper.getListById(id);
    }

    public int insertFriends(Friends friends) {
        Friends newFriend = new Friends();
        newFriend.setId(UUID.randomUUID().toString());
        newFriend.setUserId(friends.getUserId());
        newFriend.setName(friends.getName());
        Date date = new Date();
        newFriend.setCreateTime(date.getTime());
        newFriend.setOpTime(date.getTime());
        newFriend.setIsValid(1);
        int i =  friendsMapper.insertFriend(newFriend);
        return i;
    }

    public int updateFriends(Friends friends) {
        return friendsMapper.updateFriends(friends);
    }

    public int deleteFriends(String id, String userId) {
        return friendsMapper.deleteFriends(id, userId);
    }

    public List<Friends> getListByUser(String id) {
        return friendsMapper.getListByUser(id);
    }

    @Override
    public boolean getListByUserIdAndFriName(String userId, String name) {
        Friends friends = new Friends();
        friends.setName(name);
        friends.setUserId(userId);
        Friends newFriends = friendsMapper.getListByUserIdAndFriName(friends);
        if (newFriends != null)
            return true;
        return false;
    }
}
